#include "ResourceUnit.h"

using namespace cocos2d;

static const char* plist = ".plist";
static const char* pvr = ".pvr";

CResourceUnit::CResourceUnit(void)
	:m_pSpriteNode(NULL)
{
	m_strFileName.clear();
}


CResourceUnit::~CResourceUnit(void)
{
}

bool CResourceUnit::Init( const char* file )
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(!file);
		m_strFileName = file;

		std::string sTemp("");

		CCSpriteFrameCache* pCache = CCSpriteFrameCache::sharedSpriteFrameCache();
		CC_BREAK_IF(!pCache);
		sTemp = m_strFileName + plist;
		pCache->addSpriteFramesWithFile(sTemp.c_str());

		sTemp.clear();
		sTemp = m_strFileName + pvr;
		m_pSpriteNode = CCSpriteBatchNode::create(sTemp.c_str());
		CC_BREAK_IF(!m_pSpriteNode);

		bRet = true;
	} while (0);
	return bRet;
}

bool CResourceUnit::operator ==(const CResourceUnit& ru) const
{
	return ru.m_strFileName == m_strFileName ||
		ru.m_pSpriteNode == m_pSpriteNode;
}

void CResourceUnit::AddToLayer( cocos2d::CCLayer* layer, int order )
{
	if(layer && m_pSpriteNode)
	{
		layer->addChild(m_pSpriteNode,order);
	}
}

void CResourceUnit::AddSprite( cocos2d::CCSprite* sprite )
{
	if(sprite && m_pSpriteNode)
	{
		m_pSpriteNode->addChild(sprite);
	}
}
